
from datetime import datetime

# Input format is a Documentation class.

head = """<html> <head> <title>Module: %MODULE%</title> </head> <body bgcolor="#ffffff">
<p><i><a href="index.html">Table of Contents</a></i></p>
<table border="0" cellpadding="5" cellspacing="0" width="100%">
  <tr>
    <th rowspan="2" valign="top" align="left" width="10%" bgcolor="#88bbee"><font color="#000000">Module: %MODULE%</font> </th>
    <th bgcolor="#88bbee" width="90%" align="right"><font color="#000000">%FILENAME%</font> </th>
  </tr>
  <tr>
  <td>
  
  <table border="0" cellpadding="5" cellspacing="0" width="100%">"""

sectionhead = """
<tr>
      <th bgcolor="#99ccff" rowspan="2" valign="top" align="left" width="20%" > <font color="#000000"> %SECTIONNAME%&nbsp; </font> </th>
      <th bgcolor="#99ccff" valign="top" align="left" width="80%" > <font color="#000000">&nbsp;</font> </th>
    </tr>
    <tr>
      <td>%LINKS%</td>
      </tr>
"""

sectionpart = """<tr>
      <th bgcolor="#99ccff" rowspan="2" valign="top" align="left" width="20%" > <font color="#000000"> <a name="%NAME%"></a>&nbsp; </font> </th>
      <th bgcolor="#99ccff" valign="top" align="left" width="80%" > <font color="#000000">%NAME%&nbsp;</font> </th>
    </tr>
    <tr>
      <td><font color="#000088">
         %PROTOTYPE%
	%DOCSTRING%
        </font></td></tr>"""

tail = """</table>  </td>   </tr>
  
</table>
<hr>
<p><i><a href="index.html">Table of Contents</a></i></p>
<font size="-2"><i>This document was automatically generated on %DATE% by something that looks like HappyDoc but isn't really.</font>
</body> </html> """

def drawdoc(o):
	data = o.data
	strModule = o.module
	strFilename = o.filename
	
	strHtml = ''
	strHtml += head.replace('%MODULE%',strModule).replace('%FILENAME%',strFilename)
	
	
	def createLinks(a):
		s = ''
		for group in a:
			s += '<a href="#' + group[0] + '">'+group[0] + '</a><br>'
		return s
	
	for sectionType in 'methods','properties', 'functions','members':
		adata = data[sectionType]
		if not adata: continue
			
		strLinks = createLinks(adata)
		strHtml += sectionhead.replace('%LINKS%',strLinks).replace('%SECTIONNAME%',sectionType)
		
		for docpart in adata:
			docstring = format(docpart[2])
			strHtml += sectionpart.replace('%NAME%', docpart[0]).replace('%PROTOTYPE%', '<pre>'+docpart[1]+'</pre>').replace('%DOCSTRING%', docstring)
		
		
	strHtml += tail.replace('%DATE%',str(datetime.now()))
	return strHtml


def format(s):
	import re
	r = re.compile(r'\*([^\r\n*]+)\*')
	
	ss = r.sub(r'<b>\1</b>', s)
	return ss

if __name__=='__main__':
	print format('*test* is this *cool*.')
	from docs import Documentation

	d = Documentation()
	d.data['methods'] = [
		('copyThis','copyThis(a,b,c,d)','This is the doc string here.'),
		('cutThis','cutThis(a,b,c,d)','This is the *other* doc string here.'),
		]

	d.data['functions'] = []
	d.data['properties'] = [
		('bgColor','Color','This is the color of it.'),
		('fgColor','Color','This is the other doc string here.'),
		('label','String','Wow, so. Weird.'),
		]

	d.data['members'] = [
		('basecontrol','control','Uh, this should be hidden.'),
		]
	d.module = 'Best Thing Ever'
	d.filename = 'cool.py'
	res = drawdoc(d)
	print res
		